package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000118625930";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCUHyrJZnxsV6fgcq4xpWedqRXy9iM4FYSge2y1Q+Ozv+37XOGHE6s/Ezg/E9MhifBswcEtDMkoNd7lI5uddCdFjdAB5TEpDiB50DukIfAA9Up31oMb8t/8hq43U5AtH9kntkcKPeRdaiqNgjT2bhsUAODV5fJfZL/ilUVHicjTsxuq/VMEcPJ00lG91wUpi2R3lufRbC4JSUzVIIaLAi8Ywvc5Ch8eJezxurDyPcPEG8RV8hhrAMFzNuWXHTL6venSeuxkeiijuZC0sGLxQDvhanDC49Bo6oncoyyzxemlgCWBcSYFr8/fhh+H14ryjvGtgHBEWHrhpXulbEwSnvSBAgMBAAECggEBAILN/0f6JHi88/im7aPUEOvPDR4RNUrq3cOnG3n76RKstKnc/SD/QqQiAfLaPAILQXGZrVFuUBodcKhjyKHEmIW41W9HCiHaYt2QAYtRmRQPieLIYeIFmr3z5dfgCJuA25FRS+0EeS3xeoCa8tCIrYr9t4/NUDvjyfx7m8GwMNSZqBibWP+2GiAf7IkSqZQKJjL7AeTxKtxPJf0RUjks9n2FLw808tEZlp1HJY+vNLejCRt99wPP8xKijudpq1dP0MTgLCxcBNq8GR98xU9QKGbuEZOXhrdMIKRbPzCRhSFP9GB6Lx5nWjFN7GdhV7osbciN6/JG5NLEzC0llmh7oBECgYEAx0mgMPsaE+qjwkf390pgWmn8QtKh6NgbuSI+Jf+CMeSUIl1bLuE5FLWs2Qxnhrk2PVfNEEN+jtpXmphWhTp48PSfSnCP1YXYNZsY7UfZRm5bstkibo6vj16Kchtxopfe/4nwNTZLrFRM6IJng/UXDG31bW761n2HQ2atGeoHKF0CgYEAvkYJ+MQ/HMHcy/VR2ZJYUd6kBcO9Cn4RlJUdKpCbqtSdLpsO+2S2EUUA5sLL2ulpwUNowVH2vVGlIfbuVb1+zESBgvXrEj7MhKiw7hYe/PSXtTkHoLIIKeCk9YzJHdrpR+aRlmGnu8ULcZJLjVVFSveTqS9eL48sBU5rdJr/anUCgYEAwEv0/Q5u/WTeG2vig4YLL3MxbTFd9hYBukSmf3frI0WTAw3eHIBK2gbsAIwNoVw6UDrWrqjSB/n7qjrw0aG4bZk901CGGd0K4b8E8IDx0RpP9WW76b5Y/RiWssB0iBXPofhyBoabwm+dvoWGaTgnw2Ce/7ek5y89Q/Zhda3ytikCgYAcoGT3rG7iYLhbZzk9RqtLBfgfdeeHIJh8hkKxGLiS5xIO5P1IDUrnJj6QeVY6RUvH6+Bi1XYN3a6+gWwyMiRVjyj9Dwqi4BfdDZQw/dsDq7j3ZvEi2CYqRsYKu3e2qizWyHW27lJSCb9DAwOi6+VU9hKdLac5+Ixi7/1yua8b0QKBgAN2PEXT0a88sFsC5k/i2xXpUfvircHhfUKdeFSl9RxVd7zBCRVUaKi+4EYKaYYcX/VnvOVwcOXTQnfgjObW5kwC5y3k50MV6s3knml3fmtZKPSEyiHEPIh+kXm1aRAmADuxdnJK1njmUKYj9PuF3qhZqVMMYyJfsTX+8wZxew8A";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnT8Bsa0Qhi84KMzSzb653Bk6K36gfj/0VaFoS7RaKOENWaNAqz6pk0zoKLabCz9HsqRy1pFGqXkDgej5LiZS2owB53OP2QUm8NCt0gHqp/OWsCXfd3jOjaE2DP3SXsp4blEqi8B8x/nmcr781r/juI6gQMufWJUhMfcE/B284z7/6u0oM3woAw/XBQFWNHPWuIOeXB4zXmXPU79qqP4VsLaHbzESRGzpn4b7SZCGQ3y49oiSLc5/FoSZG0af0XOPDncYdBo1j81NS1g21FkZeOs3GNvjmUf8LYeCLwK/jHcSEJsiUUcO6tmFDZekpbuHBEdk1plHGokE0qW+XigkeQIDAQAB";;
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://l544208248.goho.co/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 过期时间
    private String timeOut = "30m";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    /**
     * 付款
     */
    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+ timeOut +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
